package receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.SystemClock;

import com.qb.devtest.MyApplication;

public class PowerReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        //MyUtil.showLog("PowerReceiver", "重启与关机广播");
        String action = intent.getAction();
        Context ctx = MyApplication.getInstance();
        SharedPreferences SP = ctx.getSharedPreferences("dev_test_setting", ctx.MODE_PRIVATE);
        long uptimeMillis = SystemClock.elapsedRealtime();
        long boot_time = SP.getLong("BOOT_TIME",0);
        boot_time = boot_time + uptimeMillis;
        SharedPreferences.Editor edit = SP.edit();
        edit.putLong("BOOT_TIME",boot_time);
        edit.apply();

        // android11 实测无论重启或关机获取的都是 android.intent.action.ACTION_SHUTDOWN
        if (Intent.ACTION_SHUTDOWN.equals(action)){
            //Power Off
        }else if (Intent.ACTION_REBOOT.equals(action)){
            //Power reboot
        }
    }
}
